<template>
	<div class="home-container layout-pd">
		<el-row :gutter="15" class="home-card-one mb15">
			<el-col
				:xs="24"
				:sm="12"
				:md="12"
				:lg="8"
				:xl="8"
				v-for="(v, k) in state.homeOne"
				:key="k"
				:class="{ 'home-media home-media-lg': k > 1, 'home-media-sm': k === 1 }"
			>
				<div class="home-card-item flex">
					<div class="flex-margin flex w100" :class="` home-one-animation${k}`">
						<div class="flex-auto">
							<span class="font30">{{ v.num1 }}</span>
							<!-- <span class="ml5 font16" :style="{ color: v.color1 }">{{ v.num2 }}%</span> -->
							<div class="mt10">{{ v.num3 }}</div>
						</div>
						<div class="home-card-item-icon flex" :style="{ background: `var(${v.color2})` }">
							<i class="flex-margin font32" :class="v.num4" :style="{ color: `var(${v.color3})` }"></i>
						</div>
					</div>
				</div>
			</el-col>
		</el-row>
		<div class="home-card-two mb15">
			<div class="home-card-item" ref="mapRef"></div>
		</div>
	</div>
</template>

<script setup lang="ts" name="home">
import { reactive, onMounted, ref } from 'vue';
import * as echarts from 'echarts';
import { useHome } from './home';
import { getMap } from './maputils';
// 定义变量内容
const mapRef = ref();
const zoom = ref(1);
const state = reactive({
	global: {
		homeChartOne: null,
		dispose: [null, '', undefined],
	} as any,
	homeOne: [
		{
			num1: '125,12',
			num2: '-12.32',
			num3: '设备总数',
			num4: 'fa fa-meetup',
			color1: '#FF6462',
			color2: '--next-color-primary-lighter',
			color3: '--el-color-primary',
		},
		{
			num1: '653,33',
			num2: '+42.32',
			num3: '代理商总数',
			num4: 'iconfont icon-ditu',
			color1: '#6690F9',
			color2: '--next-color-success-lighter',
			color3: '--el-color-success',
		},
		{
			num1: '125,65',
			num2: '+17.32',
			num3: '未处理工单数量',
			num4: 'iconfont icon-zaosheng',
			color1: '#6690F9',
			color2: '--next-color-warning-lighter',
			color3: '--el-color-warning',
		},
		// {
		// 	num1: '520,43',
		// 	num2: '-10.01',
		// 	num3: '访问统计信息',
		// 	num4: 'fa fa-github-alt',
		// 	color1: '#FF6462',
		// 	color2: '--next-color-danger-lighter',
		// 	color3: '--el-color-danger',
		// },
	],
});

// 批量设置 echarts resize

// 页面加载时
onMounted(() => {
	initStatistics();
	// initEchartsResize();
	initMap();
});

const datas = ref([
	{ name: '北京', value: 100 },
	{ name: '天津', value: 1000 },
	{ name: '上海', value: 100 },
	{ name: '重庆', value: 100 },
	{ name: '河北', value: 2000 },
	{ name: '河南', value: 100 },
	{ name: '云南', value: 100 },
	{ name: '辽宁', value: 100 },
]);
const initStatistics = () => {
	// useHome()
	// 	.statistics()
	// 	.then((res) => {
	// 		state.homeOne[0].num1 = res.deviceCount;
	// 		state.homeOne[1].num1 = res.dealerCount;
	// 		state.homeOne[2].num1 = res.orderCount;
	// 	});
};

const initMap = () => {
	const myChart = echarts.init(mapRef.value);
	const option = {
		// 背景颜色
		// 提示浮窗样式
		tooltip: {
			show: true,
			trigger: 'item',
			alwaysShowContent: false,
			backgroundColor: 'rgba(0, 0, 0, 0.7)',
			padding: [0, 10, 8, 10],
			roam: true,
			hideDelay: 100,
			triggerOn: 'mousemove',
			enterable: true,
			borderColor: 'rgba(0, 0, 0, 0.7)',
			//解决浮窗被遮挡
			confine: true,
			textStyle: {
				color: '#fff',
				fontSize: '12',
				width: 20,
				height: 30,
				overflow: 'break',
			},
			showDelay: 50,
			formatter: (params) => {
				return `<p style="text-align:center;line-height: 30px;height:30px;font-size: 14px;border-bottom: 1px solid #7A8698;">${params.name}</p>
              <div style="line-height:22px;margin-top:5px">设备：<span style="margin-left:12px;color:#fff;float:right">${
								params.data?.value || '--'
							}</span></div>`;
			},
		},
		// 地图配置文字显示
		geo: {
			map: 'china',
			// 禁止地图放大缩小
			roam: false,
			zoom: zoom.value,
			label: {
				// 通常状态下的样式
				normal: {
					show: true,
					textStyle: {
						color: '#333',
					},
				},
				// 鼠标放上去的样式
				emphasis: {
					show: true,
					textStyle: {
						color: '#640000',
					},
				},
			},
			// 地图区域的样式设置
			itemStyle: {
				normal: {
					borderColor: '#5d5b4c',
					borderWidth: 1,
					areaColor: '#ffe57f',
				},
				// 鼠标放上去高亮的样式
				emphasis: {
					areaColor: '#cfb20a',
					borderWidth: 0,
				},
			},
		},
		series: [
			{
				type: 'map',
				roam: true,
				zoom: 1,
				geoIndex: 0,
				label: {
					// 通常状态下的样式
					normal: {
						show: true,
						textStyle: {
							color: '#fff',
						},
					},
					// 鼠标放上去的样式
					emphasis: {
						textStyle: {
							color: '#640000',
						},
					},
				},

				data: datas.value,
			},
		],
		visualMap: {
			show: true,
			type: 'continuous',
			// 这里最大值写死了，请根据实际需求进行变更
			max: 2000,
			text: ['High', 'Low'],
			calculable: true,
			seriesIndex: [0],
			inRange: {
				// 由低到高
				color: ['#ffe57f', '#ff3456'],
			},

			textStyle: {
				color: '#fff',
				map: 'china',
			},
			bottom: '8px',
			right: '8px',
			backgroundColor: '#313E52',
			padding: [10, 8, 10, 8],
		},
		graphic: {
			type: 'text',
			x: 25,
			y: 25,
			style: {
				text: '返回',
				fill: '#fff',
				font: 'bolder 2em "Microsoft YaHei", sans-serif',
			},
			// onclick: () => {
			// 	// 利用函数的作用域，可以直接拿上面的name来用
			// 	if (this.citylist.length < 2) return;
			// 	this.lastCity = this.citylist[this.citylist.length - 2];
			// 	const name = Object.keys(this.lastCity)[0];
			// 	const [grahpName, graphJson] = getMap(name);
			// 	// 删除最后一个元素
			// 	this.citylist.pop();
			// 	// 很多操作重复了，你可以将公共部分抽离出来
			// 	option.geo.map = grahpName;
			// 	echarts.registerMap(grahpName, graphJson);
			// 	this.charts.setOption(option, true);
			// },
		},
	};

	const [mapName, mapJson] = getMap('中国');
	option.geo.map = mapName;
	// 地图注册，第一个参数的名字必须和option.geo.map一致
	echarts.registerMap(mapName, mapJson);

	myChart.setOption(option);
};
</script>

<style scoped lang="scss">
$homeNavLengh: 8;
.home-container {
	flex: 1;
	display: flex;
	flex-direction: column;
	overflow: hidden;
	.home-card-one,
	.home-card-two {
		.home-card-item {
			width: 100%;
			height: 130px;
			border-radius: 4px;
			transition: all ease 0.3s;
			padding: 20px;
			overflow: hidden;
			background: var(--el-color-white);
			color: var(--el-text-color-primary);
			border: 1px solid var(--next-border-color-light);
			&:hover {
				box-shadow: 0 2px 12px var(--next-color-dark-hover);
				transition: all ease 0.3s;
			}
			&-icon {
				width: 70px;
				height: 70px;
				border-radius: 100%;
				flex-shrink: 1;
				i {
					color: var(--el-text-color-placeholder);
				}
			}
			&-title {
				font-size: 15px;
				font-weight: bold;
				height: 30px;
			}
		}
	}
	.home-card-one {
		@for $i from 0 through 3 {
			.home-one-animation#{$i} {
				opacity: 0;
				animation-name: error-num;
				animation-duration: 0.5s;
				animation-fill-mode: forwards;
				animation-delay: calc($i/4) + s;
			}
		}
	}
	.home-card-two {
		flex: 1;
		display: flex;
		flex-direction: column;
		position: relative;
		.home-card-item {
			flex: 1;
			display: relative;
			overflow: hidden;
			.home-monitor {
				height: 100%;
				.flex-warp-item {
					width: 25%;
					height: 111px;
					display: flex;
					.flex-warp-item-box {
						margin: auto;
						text-align: center;
						color: var(--el-text-color-primary);
						display: flex;
						border-radius: 5px;
						background: var(--next-bg-color);
						cursor: pointer;
						transition: all 0.3s ease;
						&:hover {
							background: var(--el-color-primary-light-9);
							transition: all 0.3s ease;
						}
					}
					@for $i from 0 through $homeNavLengh {
						.home-animation#{$i} {
							opacity: 0;
							animation-name: error-num;
							animation-duration: 0.5s;
							animation-fill-mode: forwards;
							animation-delay: calc($i/10) + s;
						}
					}
				}
			}
		}
	}
}
</style>
